{js:my97date}
{set:$seller_id = $this->seller['seller_id']}
<script type='text/javascript' src="{theme:javascript/ajaxfileupload.js}"></script>
<article class="module width_full">
    <header>
        <h3 class="tabs_involved"> 新增优惠券</h3>
    </header>
    <form action='{url:/sellercoupon/save_coupon}' method='post'  onsubmit="return false;">
        <div class="module_content">
            <fieldset>
                <label>使用渠道：</label>
                <select name="use_channels" class="auto">
                    <option value="1">线上使用</option>
                </select>
            </fieldset>
            <fieldset>
                <label>优惠券类型：</label>
                <input name="type" value="1" type="radio" checked="checked">抵用券
            </fieldset>
            <fieldset>
                <label>优惠券名称:</label>
                <input name="name" class="large" type="text"id="coupon_name" pattern="required"  alt="优惠券名称不能为空"/>
            </fieldset>
            <fieldset>
                <label>使用范围:</label>

                <button type='button' onclick="searchGoods('{url:/block/search_seller_goods/type/radio/seller_id/$seller_id}',searchGoodsCallback);" class='btn'><span>选择商品</span></button>
                <table id='speed_goods' class='td_c'></table>
            </fieldset>
            <fieldset  id="discount">
                <label>优惠额度:</label>
                <input name="discount_money" id="discount_money" class="normal" type="text" pattern="float"  alt="只允许填写数字金额"/><label class="tip">*元</label>
            </fieldset>

            <fieldset>
                <label>使用条件:</label>
                <label><input type="radio" name="condition" onclick="$('#areaBox').hide();" value="1" checked/>不限制</label>
                <label><input type="radio" name="condition" onclick="$('#areaBox').show();" value="2"/>限制</label>
                <label id="areaBox" style='display:none'>
                    满<input class='tiny' name="full_money" value="" pattern="float" alt="只允许填写数字金额" type="text">元可用
                </label>
            </fieldset>
            <td>
                <label>有效期:</label>
                <input type="text" name='start_time' class="Wdate large" id="date_start"       alt='' onFocus="WdatePicker()" empty/>——
                <input type="text"  name='end_time'    class="Wdate large" id="date_end" onFocus="WdatePicker()" empty/>
            </td>
            <fieldset>
                <label>说明:</label>
                <input name="description"  id="description" class="large" type="text"  value="请在过期前使用" alt="该优惠券使用的注意事项"/><label class="tip">该优惠券使用的注意事项</label>
            </fieldset>
            <fieldset>
                <label>发放方式</label>
                <div class="box">
                    <label><input name="goods_sale"  id="goods_sale" value="1" type="checkbox" onclick="sale_num_check();">当成商品销售</label>
                    <!--支持保价隐藏域-->
                    <span id='goodsBox' style='display:none'>
						 <input name="goods_sale_num" value=""  id="goods_sale_num" class='tiny' pattern="int" alt="只允许填写数字" type="text" />件
					</span>
                </div>
                <div class="box">
                    <label><input name="prop_sale" id="prop_sale" value="1" type="checkbox" onclick="prop_sale_check();">作为奖品/奖励</label>
                    <!--支持保价隐藏域-->
                    <label id='propBox' style='display:none'><input type="radio"  checked/>不限制<br>
                        优惠券作为奖品奖励发放时，使用数量无法预估，故不进行限制，请确保做活动时，有充足的商品以供兑换<br>作为奖品/奖励，指该优惠券可以作为运营活动或奖励的奖品，发放给用户
                    </label>

                </div>
            </fieldset>
            <fieldset>
                <label>总数量:</label>
                <input  id="total_quantity" class="normal" type="text" disabled="disabled" alt="请填写正整数"/>
            </fieldset>
            <div id="img_box" style="display: none" >
            <fieldset>
                    <div style="float: left">
                        <div class="image_display1" >
                        </div>
                        <div data-role="fieldcontain" class="upload-box" style="width: 250px;height: 30px;margin-top: 10px;">
                            <input type="file" id="upload_img1" name="upload_img1"  value="上传"  />
                        </div>
                    </div>
                    <div style="float: left;margin-left: 50px;">
                        <div class="image_display2" >
                        </div>
                        <div data-role="fieldcontain" class="upload-box" style="width: 250px;height: 30px;margin-top: 10px;">
                            <input type="file" id="upload_img2" name="upload_img2"  value="上传"  />
                        </div>
                    </div>
                <br>
                <label>作为奖品/奖励发放时，系统提供默认的图片，可重新上传，尺寸为：160*160px 和 320*320px</label>
            </fieldset>
            </div>
        </div>
        <footer>
            <div class="submit_link">
                <input type="submit" class="alt_btn" value="确 定"/>
            </div>
        </footer>
    </form>
</article>
<script>
    function sale_num_check() {
        $('#goodsBox').toggle();
        var goods_sale = $("#goods_sale").is(":checked");
        var prop_sale = $("#prop_sale").is(":checked");
        if (goods_sale == true) {
            if (prop_sale == true) {
                $("#total_quantity").attr("value", "不限制");//填充内容
            } else {
                var goods_sale_num = $("#goods_sale_num").attr("value");
                $("#total_quantity").attr("value", goods_sale_num);//填充内容
            }
        } else {
            if (prop_sale == true) {
                $("#total_quantity").attr("value", "不限制");//填充内容
            } else {
                $("#total_quantity").attr("value", '');//填充内容
            }
        }
    }
    function prop_sale_check() {
        $('#propBox').toggle();
        $('#img_box').toggle();
        var goods_sale = $("#goods_sale").is(":checked");
        var prop_sale = $("#prop_sale").is(":checked");
        if (prop_sale == false) {
            if (goods_sale == true) {
                var goods_sale_num = $("#goods_sale_num").attr("value");
                $("#total_quantity").attr("value", goods_sale_num);//填充内容
            } else {
                $("#total_quantity").attr("value", '');//填充内容
            }
        } else {
            $("#total_quantity").attr("value", "不限制");//填充内容
        }
    }

    $('#goods_sale_num').keyup(function () {
        var goods_sale_num = $("#goods_sale_num").attr("value");
        var prop_sale = $("#prop_sale").is(":checked");
        if (prop_sale == true) {
            $("#total_quantity").attr("value", '不限制');//填充内容
        } else {
            $("#total_quantity").attr("value", goods_sale_num);//填充内容
        }
    });
    $("form").submit(function (e) {
        var goods_id = $('#goods_id').val();
        if (goods_id == null || goods_id == "") {
            alert('请选择使用范围！');
            return false;
        }

        var date_start = $('#date_start').val();
        var date_end = $('#date_end').val();
        var start = new Date(date_start.replace("-", "/").replace("-", "/"));
        var end = new Date(date_end.replace("-", "/").replace("-", "/"));
        if (date_start == null || date_start == "" || date_end == null || date_end == "") {
            alert('请选择有效起止时间！');
            return false;
        }
        var date_obj = new Date();
        var now_date = date_obj.getFullYear() + "-";
        now_date += (date_obj.getMonth() + 1) + "-";
        now_date += date_obj.getDate();
        var now = new Date(now_date);
        if (end < start) {
            alert('结束时间不能小于开始时间！');
            return false;
        }
        var name = $('#coupon_name').val().length;
        if (name < 3) {
            alert('优惠券名称不能少于3个字！');
            return false;
        }
        if (name > 30) {
            alert('优惠券名称不能多余30个字！');
            return false;
        }
        if (start < now) {
            alert('开始时间不得小于当前时间！');
            return false;
        }
        var discount_money = $('#discount_money').val();
        if (discount_money == 0) {
            alert('优惠额度请输入大于0.01的数值！');
            return false;
        }
        if (discount_money && discount_money <= 0.01) {
            alert('优惠额度请输入大于0.01的数值！');
            return false;
        }
        var discount_point = $('#discount_point').val();
        if (discount_point == 0) {
            alert('优惠额度请输入大于0.01的数值！');
            return false;
        }
        if (discount_point && discount_point <= 0.01) {
            alert('优惠额度请输入大于0.01的数值！');
            return false;
        }
        var description_length = $('#description').val().length;
        if (description_length > 50) {
            alert('优惠券使用说明限制50个字符！');
            return false;
        }
        var goods_sale = $("#goods_sale").is(":checked");
        var prop_sale = $("#prop_sale").is(":checked");
        if (goods_sale == false && prop_sale == false) {
            alert('请至少选择一种发放方式!');
            return false;
        }
        if(prop_sale){
            var image1 = $('#image1').val();
            var image2 = $('#image2').val();
            if (image1 == null || image1 == "" || image2 == null || image2 == "") {
                alert('作为奖品/奖励，图片必须上传！');
                return false;
            }
        }
        return true;
    });

    //输入筛选商品的条件
    function searchGoodsCallback(goodsList) {
        goodsList.each(function () {
            var temp = $.parseJSON($(this).attr('data'));
            var content = {
                "data":
                    {
                        "id": temp.goods_id,
                        "name": temp.name,
                        "sell_price": temp.sell_price,
                        "store_nums": temp.store_nums,
                        "pay_point":temp.pay_point,
                    }
            };
            relationCallBack(content);
        });
    }

    //关联商品回调处理函数
    function relationCallBack(content) {
        if (content) {
            var imgUrl = "{webroot:@url@}";
            imgUrl = imgUrl.replace("@url@", content['data']['img']);

            var goods_id = content['data']['id'];
            var html = '<td>' + content['data']['name'] + '</td>'
                + '<td>价格：' + content['data']['sell_price'] + '</td>'
                + '<td>积分：' + content['data']['pay_point'] + '</td>'
                + '<td>库存：' + content['data']['store_nums'] + '</td>';
            $('#speed_goods').html(html);
            $("#speed_goods").append("<input type='hidden' name='goods_id'  id='goods_id' />");
            $("#goods_id").val(goods_id);
            if (content['data']['sell_price'] == 0) {
                $("#speed_goods").append("<input type='hidden' name='use_type'  id='use_type' value='2' />");
                var fullHtml = "满<input class='tiny' name='full_point' value='' pattern='float' alt='只允许填写数字' type='text'>积分可用";
                var discountHtml="<label>优惠额度:</label><input name='discount_point' id='discount_point' class='normal' type='text' pattern='float'  alt='只允许填写数字'/><label class='tip'>*积分</label>";
            } else {
                $("#speed_goods").append("<input type='hidden' name='use_type'  id='use_type' value='1' />");
                var fullHtml = "满<input class='tiny' name='full_money' value='' pattern='float' alt='只允许填写数字金额' type='text'>元可用";
                var discountHtml="<label>优惠额度:</label><input name='discount_money' id='discount_money' class='normal' type='text' pattern='float'  alt='只允许填写数字金额'/><label class='tip'>*元</label>";

            }
            $('#areaBox').html(fullHtml);
            $('#discount').html(discountHtml);
        }
    }
    //照片异步上传
    $('#upload_img1').change(function () {  //此处用了change事件，当选择好图片打开，关闭窗口时触发此事件
        $.ajaxFileUpload({
            url: '{url:/sellercoupon/save_images}',   //处理图片的脚本路径
            type: 'post',       //提交的方式
            fileElementId: 'upload_img1',     //file控件ID
            secureuri: false,
            dataType: 'json',  //服务器返回的数据类型
            success: function (data) {  //提交成功后自动执行的处理函数
                $('.image_display1').empty();
                if(data.code=='success'){
                    ///此处效果是：当成功上传后会返回一个json数据，里面有url，取出url赋给img标签，然后追加到类里显示出图片
                    $('.image_display1').append('<img src="' + data.url + '" value="' + data.url + '" style="width:250px;height: 160px"><input type="hidden" name="image1" id="image1" value="'+data.url +'">');
                }
            }
        })
        return true;
    });
    //照片异步上传
     $('#upload_img2').change(function () {  //此处用了change事件，当选择好图片打开，关闭窗口时触发此事件
        $.ajaxFileUpload({
            url: '{url:/sellercoupon/save_images}',   //处理图片的脚本路径
            type: 'post',       //提交的方式
            fileElementId: 'upload_img2',     //file控件ID
            secureuri: false,
            dataType: 'json',  //服务器返回的数据类型
            success: function (data) {  //提交成功后自动执行的处理函数
                $('.image_display2').empty();
                if(data.code=='success'){
                    ///此处效果是：当成功上传后会返回一个json数据，里面有url，取出url赋给img标签，然后追加到类里显示出图片
                    $('.image_display2').append('<img src="' + data.url + '" value="' + data.url + '" style="width:250px;height: 160px"><input type="hidden" name="image2" id="image2" value="'+data.url+'">');
                }
            }
        })
        return true;
    });
</script>